<?php

require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

require_once(DIR_LIB_VSCORE."db.php");

class VsProduct {

	var $_db = NULL;
	var $_sqlTable='products';

	var $_id = 0;
	var $_name = '';
	var $_info = '';
	private $_image = '';
	private $_price = 0.0;
	var $_date='';

	var $_orderBy = 0;
	var $_enabled = FALSE;

	var $_categoryId = 0;

	function VsProduct() {
		$this->_db = VsDatabase::Instance();

		$this->_sqlTable = $this->_sqlTable."_".strtolower(VsSession::Instance()->value('lang'));
	}
	//==============================================================================

	function install()  {

	}
	//==============================================================================

	function id() {
		return $this->_id;
	}
	//=============================================================================

	function categoryId() {
		return $this->_categoryId;
	}
	//=============================================================================

	function name() {
		return $this->_name;
	}
	//=============================================================================

	function price() {
		return $this->_price;
	}
	//=============================================================================

	function info() {
		//return nl2br($this->_info);
		return preg_replace('/\s+/', ' ', nl2br($this->_info));
	}
	//=============================================================================

	function image() {
		return $this->_image;
	}
	//=============================================================================

	function save() {
		//echo "VsProduct::save()";

		if($this->_id > 0)
			$this->update();
		else
			$this->insert();
	}
	//==============================================================================

	function delete()  {
		$q = "DELETE FROM `".$this->_sqlTable."` WHERE id=".$this->_id;

		$this->_db->query($q);
	}
	//==============================================================================

	private function insert()  {
		$q = "INSERT INTO `".$this->_sqlTable."` (`products_categories_id`, `name`, `info`, `date`) VALUES('".$this->_categoryId."', '".addslashes($this->_name)."', '".addslashes($this->_info)."', NOW())";
		//echo $q;

		$this->_db->query($q);
		//echo mysql_error();

		$this->_id = mysql_insert_id();
	}
	//==============================================================================

	private function update()  {
		$q = "UPDATE `".$this->_sqlTable."` SET `products_categories_id`='".$this->_categoryId."', `name`='".addslashes($this->_name)."', `info`='".addslashes($this->_info)."', date=NOW() WHERE id=".$this->_id;

		$this->_db->query($q);
	}
	//==============================================================================


};

?>
